<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Step extends MX_Controller {
    
  function __construct() {  
    $this->load->library('template');
    $this->load->library('form_eksternal');
    $this->load->library('global_models');
    $this->load->library('PHPExcel');
    $this->load->library('encrypt');
    $this->load->helper('path');
    $this->load->helper('url');
    $this->load->model('dev_checklist/mstep');
    $this->load->library('parser');
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->load->library('pagination');
    $this->menu = $this->cek();
  }
  public function add_new($id = 0, $pesan = "hal"){
    $this->template->title('Sistem', "Add Edit");
    if($id > 0){
      $data_detail = $this->global_models->get("m_dev_checklist", array("id_dev_step" => $id));
//      $this->debug($data_detail, true);
    }
    else{
      $data_detail[0]->id_dev_step = 0;
      $data_detail[0]->name = "";
    }
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('add-new-step', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Step',
              'detail'  => $data_detail,
              'menu_drop'   => $this->menu,
              'menu'        => "settings",
              'foot'    => '
                <script>
                  $(function () {
                    // validate signup form on keyup and submit
                    var validator = $("#formstep").validate({
                        rules: {
                            name: "required"
                        },
                        messages: {
                            name: "Enter name"
                        }
                    });
                });
                </script>
                '
            ));
      $this->template
        ->set_layout('default')
        ->build('add-new-step');
    }
    else{
      if($this->input->post("id_detail", TRUE)){
        $kirim = array(
              "name"                  =>  $this->input->post("name", TRUE),
              "urut"                  =>  $this->input->post("urut", TRUE),
              "update_by_users"       =>  $this->session->userdata('id')
          );
       
        $hasil_in = $this->global_models->update("m_dev_checklist", array("id_dev_step" => $this->input->post("id_detail", TRUE)), $kirim);
       
        if($hasil_in){
          redirect ("dev_checklist/step/index/list/sukses");
        }
        else
          redirect ("dev_checklist/step/index/list/filed");
      }
      else{
        $pst = $this->input->post(NULL, TRUE);
//        $pst = $pst['addressform']['addressform'][0];
        if($pst['name']){
          $kirim = array(
              "name"                  =>  $pst['name'],
              "urut"                  =>  $pst['urut'],
              "create_by_users"       =>  $this->session->userdata('id'),
              "create_date"           =>  date("Y-m-d H:i:s"),
              "update_by_users"       =>  $this->session->userdata('id')
          );
          if($this->global_models->insert("m_dev_checklist", $kirim))
            redirect ("dev_checklist/step/index/list/sukses");
          else{
            redirect ("dev_checklist/step/index/list/filed");
          }
        }
        else{
          redirect ("dev_checklist/step/index/list/filed");
        }
      }
    }
  }
  
  public function sort(){
     
       $items = $this->input->post('sortid');
       $itemsurut = $this->input->post('nourut');
       
       $total_items = count($this->input->post('sortid'));
        $no = 1;
     
         for($item = 0; $item < $total_items; $item++ )
         {
              $whr = array(
            "id_dev_step"                  =>  $items[$item]
        );
               $updt = array(
            "urut"                  =>  $no
        );
               
        $hasil_in = $this->global_models->update("m_dev_checklist", $whr, $updt);
         $no++;
          } 
        
       echo json_encode($items);
    die;
  }
  /**
   * ini delete
   * @param int $id id yang didelete
   * @return true redirect
   * @
   */
	public function delete($id){
    if($this->global_models->delete("m_dev_checklist", array("id_dev_step" => $id)))
      redirect("dev_checklist/step/index/list/sukses");
    else
      redirect("dev_checklist/step/index/list/filed");
  }
	public function export_xls(){
    $this->musers->export_xls("data-users");
  }
	public function index($action = "list", $pesan = "hal", $hal = 0){
            
   
    $list = $this->mstep->get("urut","m_dev_checklist");
    $this->template->title('Sistem', "Development Checklist");
    
    $this->template->build('main', 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'data'        => $list,
            'menu_drop'   => $this->menu,
            'menu'        => "settings",
            'title_table' => "Menu Checklist Table",
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build('main');
	} 
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */